package com.xyb.service;

import com.xyb.dao.CourseDao;
import com.xyb.entity.Course;
import com.xyb.entity.CourseQuery;
import com.xyb.utils.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CourseService {
    @Resource
    private IdUtils idUtils;
    @Resource
    private CourseDao courseDao;

    public String insertCourse(Course course) {
        long nextId = idUtils.nextId();
        course.setCourseId(nextId);
        course.setStatus(1);
        course.setCreateTime(new Date());
        course.setCreateUser("//todo"); //todo
        courseDao.insertCourse(course);
        return "success";
    }

    public String updateCourseByCourseId(Course course) {
        courseDao.updateCourseByCourseId(course);
        return "success";
    }

    public List<Course> selectCourseList(CourseQuery courseQuery) {
        List<Course> list = courseDao.selectCourseListByCourseQuery(courseQuery);
        return list;
    }

    public Course selectCourseByCourseId(Long courseId) {
        Course course = courseDao.selectCourseByCourseId(courseId);
        return course;
    }

    public String deleteCourseByCourseId(Long courseId) {
        courseDao.deleteCourseByCourseId(courseId);
        return "success";
    }

    public String updateCourseOrderByCourseId(Long[] courseIds) {
        List<Course> list = new ArrayList<>();
        for (int i = 0; i < courseIds.length; i++) {
            Course course = new Course();
            course.setCourseId(courseIds[i]);
            course.setShowOrder(i);
            list.add(course);
        }
        courseDao.updateCourseByCourseList(list);
        return "success";
    }
}
